﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Windows.Controls;
using System.Threading.Tasks;
using System.Threading;
using System.Globalization;

namespace PracaInz_v0
{
    class LoadData
    {
        public static DataTable loadFile(string source)         // wczytuje plik do tablicy
        {
            CultureInfo ci = new System.Globalization.CultureInfo("en-us");
            Thread.CurrentThread.CurrentCulture = ci;
            DataTable Tab = new DataTable();
            StreamReader sr = new StreamReader(source);
            DataRow row;

            string s = "";
            int k = 0; 
            string[] s2;
            while ((s = sr.ReadLine()) != null)
            {
                row = Tab.NewRow();
                s2 = s.Split(new char[] { ' ', '\t', ';' }, StringSplitOptions.RemoveEmptyEntries);
                if (k == 0)
                {
                    for (int i = 0; i < s2.Length; i++)
                    {
                        Tab.Columns.Add(s2[i], typeof(double));
                    }                    
                }
                else
                {
                    for (int i = 0; i < s2.Length; i++)
                    {
                        s2[i] = s2[i].Replace(",", ".");
                        row[i] = Math.Round(double.Parse(s2[i]), 4);
                    }
                    Tab.Rows.Add(row);
                }
                k++;
            }

            sr.Close();

            return Tab;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public static DataTable loadFile(string source, System.Windows.Controls.TextBox tX, System.Windows.Controls.TextBox tY)         // wczytuje plik do tablicy
        {
            CultureInfo ci = new System.Globalization.CultureInfo("en-us");
            Thread.CurrentThread.CurrentCulture = ci;
            DataTable Tab = new DataTable();
            StreamReader sr = new StreamReader(source);
            DataRow row;

            bool first = true;
            string s = "";
            string[] s2;
            while ((s = sr.ReadLine()) != null)
            {
                row = Tab.NewRow();
                s2 = s.Split(new char[] { ' ', '\t', ';' }, StringSplitOptions.RemoveEmptyEntries);

                if (first)
                {
                    for (int i = 0; i < s2.Length - 1; i++)
                    {
                        Tab.Columns.Add("x" + (i + 1).ToString(), typeof(double));
                    }
                    Tab.Columns.Add("Y", typeof(double));
                    first = false;
                }

                for (int i = 0; i < s2.Length; i++)
                {
                    s2[i] = s2[i].Replace(",", ".");
                    row[i] = Math.Round(double.Parse(s2[i]), 4);
                }
                Tab.Rows.Add(row);
            }

            sr.Close();
            tX.Text = (Tab.Columns.Count - 1).ToString();
            tY.Text = Tab.Rows.Count.ToString();
            return Tab;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public static DataTable loadFileVect(string source)         // wczytuje plik do tablicy
        {
            CultureInfo ci = new System.Globalization.CultureInfo("en-us");
            Thread.CurrentThread.CurrentCulture = ci;
            DataTable Tab = new DataTable();
            StreamReader sr = new StreamReader(source);
            DataRow row;

            string s = "";
            string[] s2;
            bool first = true;

            while ((s = sr.ReadLine()) != null)
            {
                row = Tab.NewRow();
                s2 = s.Split(new char[] { ' ', '\t', ';' }, StringSplitOptions.RemoveEmptyEntries);

                if (first)
                {
                    for (int i = 0; i < s2.Length - 1; i++)
                    {
                        Tab.Columns.Add("x" + (i + 1).ToString(), typeof(double));
                    }
                    Tab.Columns.Add("Y", typeof(double));
                    first = false;
                }

                for (int i = 0; i < s2.Length; i++)
                {
                    s2[i] = s2[i].Replace(",", ".");
                    row[i] = Math.Round(double.Parse(s2[i]), 4);
                }
                Tab.Rows.Add(row);
            }

            sr.Close();

            return Tab;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public static DataTable loadFileVect2(string source)         // wczytuje plik do tablicy
        {
            CultureInfo ci = new System.Globalization.CultureInfo("en-us");
            Thread.CurrentThread.CurrentCulture = ci;
            DataTable Tab = new DataTable();
            StreamReader sr = new StreamReader(source);
            DataRow row;

            string s = "";
            string[] s2;
            bool first = true;

            while ((s = sr.ReadLine()) != null)
            {
                row = Tab.NewRow();
                s2 = s.Split(new char[] { ' ', '\t', ';' }, StringSplitOptions.RemoveEmptyEntries);

                if (first)
                {
                    for (int i = 0; i < s2.Length; i++)
                    {
                        Tab.Columns.Add("x" + (i + 1).ToString(), typeof(double));
                    }
                    //Tab.Columns.Add("Y", typeof(double));
                    first = false;
                }
                else
                {
                    for (int i = 0; i < s2.Length; i++)
                    {
                        s2[i] = s2[i].Replace(",", ".");
                        row[i] = Math.Round(double.Parse(s2[i]), 4);
                    }
                    Tab.Rows.Add(row);
                }
            }

            sr.Close();

            return Tab;
        }        

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public static int[] forTreeSize(DataTable t) //zwraca 2 elementy : 1 - level , 2 - maxNode (masymalna ilosc elementow w poziomie)
        {
            int[] fts = new int[2];
            DataTable tab = t;
            
            fts[0] = Convert.ToInt32(tab.Rows[tab.Rows.Count-1]["Level"]);

            int maxNode = 0;

            for (int i = 0; i < tab.Rows.Count; i++)
            {
                int tmp = Convert.ToInt32(tab.Rows[i][2]);
                if(tmp > maxNode)
                    maxNode = tmp;
            }
            fts[1] = maxNode;

            return fts;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public static int forNetSize(DataTable t)       // zwraca maksymala ilosc neuronow w warstwie (do rozmiarow canvasa)
        {
            DataTable tab = t;
            int max = 0;

            for (int i = 0; i < tab.Rows.Count; i++)
            {
                int tmp = Convert.ToInt32(tab.Rows[i][1]);
                if (tmp > max)
                    max = tmp;
            }

            return max;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public static int[] neuronsInLayer(DataTable t)     // zlicza ile jest neuronow w danej warstwie
        {
            DataTable tab = t;
            int[] layers = new int [Convert.ToInt32(tab.Rows[tab.Rows.Count-1]["L"])];

            int l = 1;      // warstwa
            int i = 0;      // ilosc

            foreach (DataRow row in tab.Rows)
            {
                if (Convert.ToInt32(row[0]) == l)
                {
                    i++;
                }
                else 
                {
                    layers[l - 1] = i;
                    l++;
                    i = 1;
                }
            }
            layers[l - 1] = i;

            return layers;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public static TextBox vectorList(int nr)           // wczytuje plik do textboxa
        {
            TextBox t = new TextBox();
            string path = "..//..//..//..//tmp//" + nr.ToString() + ".txt";
            string line;
            try
            {
                // Ensure that the target does exist.
                if (File.Exists(path))
                {
                    StreamReader sr = new StreamReader(path);
                    while ((line = sr.ReadLine()) != null)
                    {
                        t.Text += line + Environment.NewLine;
                    }
                }
                else 
                {
                    t.Text = "File not found!";
                }
 
            }
            catch (Exception e)
            {
                System.Windows.MessageBox.Show("Operation failed: {0}", e.ToString());
            }

            return t;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public static void save(DataTable tab, string name, string separator)          // zapisuje dane datatable do pliku 
        {
            StreamWriter sw = new StreamWriter(name);
            string sep = separator;
            if (sep == "\\t")
                sep = "\t";
            for (int i = 0; i < tab.Columns.Count; i++)
            {
                if(i ==0)
                    sw.Write(tab.Columns[i].ColumnName);
                else
                    sw.Write(sep + tab.Columns[i].ColumnName);
            }
            sw.WriteLine();
            for (int i = 0; i < tab.Rows.Count; i++)
            {
                for (int j = 0; j < tab.Columns.Count; j++)
                {
                    if (j == 0)
                        sw.Write(tab.Rows[i][j]);
                    else
                        sw.Write(sep + tab.Rows[i][j]);
                }
                sw.WriteLine();
            }
            sw.Close();
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


        static public void showDataTable(DataTable tab, TextBox text)        // wypisuje datatable do texboxa
        {
            //TextBox text = new TextBox();

            for (int i = 0; i < tab.Columns.Count; i++)
            {
                text.Text += tab.Columns[i].ColumnName + "\t";
            }

            text.Text += Environment.NewLine;

            for (int i = 0; i < tab.Rows.Count; i++)
            {
                for (int j = 0; j < tab.Columns.Count; j++)
                {
                    text.Text += tab.Rows[i][j].ToString() + "\t";
                }
                text.Text += Environment.NewLine;
            }
            text.Text += tab.Rows.Count;        // pomocnicze
            //return text;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        static public DataTable LoadNodeVectSource(string source)           //wczytuje sciezki do plikow z wektorami wezlow
        {
            DataTable Tab = new DataTable();
            Tab.Columns.Add("ID", typeof(int));
            Tab.Columns.Add("Source", typeof(string));

            StreamReader sr = new StreamReader(source);
            DataRow row;

            string s = "";
            int k = 0;
            string[] s2;
            while ((s = sr.ReadLine()) != null)
            {
                row = Tab.NewRow();
                s2 = s.Split(new char[] { ' ', '\t', ';' }, StringSplitOptions.RemoveEmptyEntries);
                if (k != 0)
                {
                    for (int i = 0; i < s2.Length; i++)
                    {
                        row[i] = s2[i];
                    }
                    Tab.Rows.Add(row);
                }
                k++;
            }

            sr.Close();

            return Tab;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        static public TextBox showDataTable(DataTable tab)        // wypisuje datatable do texboxa
        {
            TextBox text = new TextBox();

            for (int i = 0; i < tab.Columns.Count; i++)
            {
                text.Text += tab.Columns[i].ColumnName + "\t";
            }

            text.Text += Environment.NewLine;

            for (int i = 0; i < tab.Rows.Count; i++)
            {
                for (int j = 0; j < tab.Columns.Count; j++)
                {
                    text.Text += tab.Rows[i][j].ToString() + "\t";
                }
                text.Text += Environment.NewLine;
            }
            text.Text += tab.Rows.Count;        // pomocnicze
            return text;
        }
    }
}
